<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业ID" prop="companyId">
        <el-input v-model="formData.companyId" placeholder="请输入企业ID" />
      </el-form-item>
      <el-form-item label="员工ID" prop="employeeId">
        <el-input v-model="formData.employeeId" placeholder="请输入员工ID" />
      </el-form-item>
      <el-form-item label="员工名称" prop="employeeName">
        <el-input v-model="formData.employeeName" placeholder="请输入员工名称" />
      </el-form-item>
      <el-form-item label="岗位ID" prop="postId">
        <el-input v-model="formData.postId" placeholder="请输入岗位ID" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="formData.postName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="发放积分" prop="score">
        <el-input v-model="formData.score" placeholder="请输入发放积分" />
      </el-form-item>
      <el-form-item label="规则ID" prop="ruleId">
        <el-input v-model="formData.ruleId" placeholder="请输入规则ID" />
      </el-form-item>
      <el-form-item label="规则名称" prop="ruleName">
        <el-input v-model="formData.ruleName" placeholder="请输入规则名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CompanyScoreSendRecordApi, CompanyScoreSendRecordVO } from '@/api/dlbz/companyscoresendrecord'

/** 企业积分发放记录 表单 */
defineOptions({ name: 'CompanyScoreSendRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  companyId: undefined,
  employeeId: undefined,
  employeeName: undefined,
  postId: undefined,
  postName: undefined,
  score: undefined,
  ruleId: undefined,
  ruleName: undefined
})
const formRules = reactive({
  companyId: [{ required: true, message: '企业ID不能为空', trigger: 'blur' }],
  employeeId: [{ required: true, message: '员工ID不能为空', trigger: 'blur' }],
  employeeName: [{ required: true, message: '员工名称不能为空', trigger: 'blur' }],
  postId: [{ required: true, message: '岗位ID不能为空', trigger: 'blur' }],
  postName: [{ required: true, message: '岗位名称不能为空', trigger: 'blur' }],
  score: [{ required: true, message: '发放积分不能为空', trigger: 'blur' }],
  ruleId: [{ required: true, message: '规则ID不能为空', trigger: 'blur' }],
  ruleName: [{ required: true, message: '规则名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CompanyScoreSendRecordApi.getCompanyScoreSendRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CompanyScoreSendRecordVO
    if (formType.value === 'create') {
      await CompanyScoreSendRecordApi.createCompanyScoreSendRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await CompanyScoreSendRecordApi.updateCompanyScoreSendRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    companyId: undefined,
    employeeId: undefined,
    employeeName: undefined,
    postId: undefined,
    postName: undefined,
    score: undefined,
    ruleId: undefined,
    ruleName: undefined
  }
  formRef.value?.resetFields()
}
</script>